package Access;

import DAO.SimpleAccessorFactory;
import Entities.Executor;
import Entities.ExecutorWithInstrument;
import Entities.Group;
import Entities.Instrument;
import Entities.LinkExecutorWithInstrumentAndGroup;
import Exceptions.InvalidEntityException;
import ExtEntities.VeryExtExecutor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;

/**
 *
 * @author 647_2
 */
@Stateless
public class Executors implements ExecutorsRemote {
    

    public LinkedList<Executor> getAllExecutors() {
        try {
            Executor ex = new Executor();
            return SimpleAccessorFactory.createSimpleAccessor(ex).select(ex);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public LinkedList<Executor> getAllExecutors(int start, int count) {
    try {
            Executor ex = new Executor();
            return SimpleAccessorFactory.createSimpleAccessor(ex).select(ex, start, count);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(GroupsRemote.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public Executor getExecutor(int ID){
        try {
            Executor ex = new Executor(ID, null, null, null);
            LinkedList<Executor> temp = SimpleAccessorFactory.createSimpleAccessor(ex).select(ex);

            if (temp.size() > 0){
                return temp.get(0);
            } else {
                return null;
            }

        } catch (InvalidEntityException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (InstantiationException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IOException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public VeryExtExecutor getFullExecutor(int ID){
        try {
            Executor ex = new Executor(ID, null, null, null);
            LinkedList<Executor> temp = SimpleAccessorFactory.createSimpleAccessor(ex).select(ex);

            if (temp.size() > 0){

                //ExecutorWithInstrument ExecutorWithInstrument(ex, null)

                //LinkExecutorWithInstrumentAndGroup lewiag = new LinkExecutorWithInstrumentAndGroup(new ExecutorWithInstrument(ex, null), null);
                //LinkedList<LinkExecutorWithInstrumentAndGroup> lewiags = SimpleAccessorFactory.createSimpleAccessor(lewiag).select(lewiag);
                //<Group> groups = new LinkedList<Group>();
                //for (int i = 0; i < lewiags.size(); i++){
                //    groups.add(lewiags.get(i).get_group());
                //    System.out.println("Группа у " + temp.get(0).get_FIO() + ": " + lewiags.get(i).get_group().get_name());
                //}

                LinkedList<Group> groups = new LinkedList<Group>();

                ExecutorWithInstrument ewi = new ExecutorWithInstrument(ex, null);
                LinkedList<ExecutorWithInstrument> ewis = SimpleAccessorFactory.createSimpleAccessor(ewi).select(ewi);
                LinkedList<Instrument> instrs = new LinkedList<Instrument>();
                for (int i = 0; i < ewis.size(); i++){
                    instrs.add(ewis.get(i).get_instrument());

                    LinkExecutorWithInstrumentAndGroup lewiag = new LinkExecutorWithInstrumentAndGroup(ewis.get(i), null);
                    LinkedList<LinkExecutorWithInstrumentAndGroup> lewiags = SimpleAccessorFactory.createSimpleAccessor(lewiag).select(lewiag);

                    for (int j = 0; j < lewiags.size(); j++){
                        groups.add(lewiags.get(j).get_group());
                    }
                }

                VeryExtExecutor vee = new VeryExtExecutor(ID, temp.get(0).get_FIO(), temp.get(0).get_dateOfBirth(), temp.get(0).get_info(), groups, instrs);

                return vee;
            } else {
                return null;
            }

        } catch (InvalidEntityException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (InstantiationException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IOException ex) {
            Logger.getLogger(Groups.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public boolean AddExecutor(String FIO, Date date, String info){
        try {
            Executor ex = new Executor(FIO, date, info);
            return SimpleAccessorFactory.createSimpleAccessor(ex).insert(ex);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public boolean UpdateExecutor(int ID, String FIO, Date date, String info){
        try {
            Executor ex = new Executor(ID, null, null, null);
            Executor newex = new Executor(FIO, date, info);
            return SimpleAccessorFactory.createSimpleAccessor(ex).update(ex, newex);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public boolean AddInstrumentToExecutor(int ID, int iID){
        try {
            ExecutorWithInstrument ewi = new ExecutorWithInstrument(new Executor(ID, null, null, null), new Instrument(iID, null, null, null));
            return SimpleAccessorFactory.createSimpleAccessor(ewi).insert(ewi);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public boolean DeleteInstrumentOfExecutor(int ID, int iID){
        try {
            ExecutorWithInstrument ewi = new ExecutorWithInstrument(new Executor(ID, null, null, null), new Instrument(iID, null, null, null));
            return SimpleAccessorFactory.createSimpleAccessor(ewi).delete(ewi);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }


    public boolean DeleteExecutor(int ID){
        try {
            Executor ex = new Executor(ID, null, null, null);
            return SimpleAccessorFactory.createSimpleAccessor(ex).delete(ex);
        } catch (InvalidEntityException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Compositions.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }
 
}
